﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueLotus7.Contract;
using BlueLotus7.Common;

namespace BlueLotus7.Contract
{
    public class BaseMessage : IValidate
    {
        protected Guid key;
        protected Guid targetKey;
        protected Guid? ownerKey;
        protected string content;
        protected DateTime createdStamp;

        #region Property

        /// <summary>
        /// The key.
        /// </summary>
        public Guid Key
        {
            get { return key; }
        }

        /// <summary>
        /// The target key.
        /// </summary>
        public Guid TargetKey
        {
            get { return targetKey; }
        }

        /// <summary>
        /// The owner key.
        /// </summary>
        public Guid? OwnerKey
        {
            get { return ownerKey; }
            set { ownerKey = value; }
        }

        /// <summary>
        /// The content.
        /// </summary>
        public string Content
        {
            get { return content; }
        }

        /// <summary>
        /// The created stamp.
        /// </summary>
        public DateTime CreatedStamp
        {
            get { return createdStamp; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="targetKey">The target key.</param>
        /// <param name="content">The content.</param>
        public BaseMessage(Guid targetKey, string content)
        {
            this.content = content;
            this.targetKey = targetKey;
            this.key = Guid.NewGuid();
            this.createdStamp = DateTime.Now;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="targetKey">The target key.</param>
        /// <param name="content">The content.</param>
        /// <param name="createdStamp">The created stamp.</param>
        public BaseMessage(Guid key, Guid targetKey, string content, DateTime createdStamp)
        {
            this.key = key;
            this.targetKey = targetKey;
            this.content = content;
            this.createdStamp = createdStamp;
        }

        #endregion

        #region IValidate

        public bool IsValid
        {
            get
            {
                try
                {
                    Validate();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public void Validate()
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                throw new InvalidObjectException("content");
            }
            else if (Validator.IsZeroDateTime(createdStamp))
            {
                throw new InvalidObjectException("createdStamp");
            }
        }

        #endregion
    }
}
